package com.talkweb.newproject.admin.sysuser.service;

import com.talkweb.newproject.admin.sysmenu.dto.SysMenuDTO;
import com.talkweb.newproject.admin.sysmenu.service.SysMenuService;
import com.talkweb.newproject.admin.sysresource.dto.SysResourceDTO;
import com.talkweb.newproject.admin.sysresource.service.SysResourceService;
import com.talkweb.newproject.admin.sysrole.dto.SysRoleDTO;
import com.talkweb.newproject.admin.sysrole.service.SysRoleService;
import com.talkweb.newproject.admin.sysuser.dao.SysUserDao;
import com.talkweb.newproject.admin.sysuser.dto.SysUserDTO;
import com.talkweb.newproject.api.model.admin.SysResourceVo;
import com.talkweb.newproject.api.model.admin.SysUserParam;
import com.talkweb.newproject.api.model.shiro.LoginInfoVo;
import com.talkweb.newproject.api.model.shiro.RolePermissionVo;
import com.talkweb.newproject.api.model.shiro.SysMenuVo;
import com.talkweb.newproject.api.model.shiro.SysRoleVo;
import com.talkweb.newproject.common.utils.MapperUtil;
import org.apache.commons.collections.map.HashedMap;
import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qsf on 2019/4/17.
 */
@Service
public class SysUserService implements Serializable {

    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    MapperUtil mapperUtil;
    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    SysResourceService sysResourceService;
    @Autowired
    SysMenuService sysMenuService;
    /**
     * 新增用户
     */
    @Transactional(rollbackFor = {Exception.class})
    public Long inserSysUser(SysUserDTO sysUserDTO) throws Exception {
        Map<String,Object> paramsMap = new HashMap<>();
        paramsMap.put("dto",sysUserDTO);
        int count = sysUserDao.insertSysUser(paramsMap);
        SysUserDTO resultDTO = (SysUserDTO) paramsMap.get("dto");
        Long keyId = resultDTO.getId();
        return keyId;
    }

    /**
     * 更新用户信息
     */
    @Transactional(rollbackFor = {Exception.class})
    public void updateSysUser(SysUserDTO sysUserDTO) throws Exception {
        Map<String,Object> paramsMap = new HashedMap();
        paramsMap.put("dto",sysUserDTO);
        sysUserDao.updateSysUser(paramsMap);
    }

    /**
     * 删除用户信息
     */
    public void deleteSysUserByPrimaryKey(String ids) throws Exception{
        Map<String,Object> paramsMap = new HashedMap();
        paramsMap.put("ids",ids);
        sysUserDao.deleteSysUserByPrimaryKey(paramsMap);
    }

    /**
     * 根据主键查询用户信息
     */
    public SysUserDTO querySysUserByPrimaryKey(String id) throws Exception{
        SysUserDTO dto = sysUserDao.querySysUserByPrimaryKey(id);
        if(dto==null) {
            dto = new SysUserDTO();
        }
        return dto;
    }

    /**
     * 根据用户名查询用户信息
     */
    public SysUserDTO getUserInfoByLoginName(SysUserDTO sysUserDTO) throws Exception {
        Map<String,Object> paramsMap =new HashedMap();
        paramsMap.put("dto",sysUserDTO);
        return sysUserDao.getUserInfoByLoginName(paramsMap);
    }

    /**
     * 返回登录信息
     * @param userId
     * @return
     * @throws Exception
     */
    public LoginInfoVo getLoginInfoByUserId(String userId) throws Exception {
        LoginInfoVo loginInfoVo = new LoginInfoVo();
        List<SysRoleDTO> sysRoleDTOList=this.getSysRoleByUserId(userId);
        List<SysResourceDTO> sysResourceDTOList=this.getPermission(userId);
        List<SysMenuDTO> sysMenuDTOList = this.getSysMenuByUserId(userId);

        List<SysRoleVo> sysRoleVoList = mapperUtil.map(sysRoleDTOList, SysRoleVo.class);
        List<RolePermissionVo> rolePermissionVoList = mapperUtil.map(sysResourceDTOList, RolePermissionVo.class);
        List<SysMenuVo> sysMenuVoList = mapperUtil.map(sysMenuDTOList, SysMenuVo.class);

        sysMenuVoList=this.convertSysMenuVo(sysMenuVoList);

        loginInfoVo.setSysRoleVoList(sysRoleVoList);
        loginInfoVo.setRolePermissionVoList(rolePermissionVoList);
        loginInfoVo.setSysMenuVoList(sysMenuVoList);
        return loginInfoVo;
    }


    /**
     * 根据用户id查询用户角色集合
     * @param userId
     * @return
     */
    private List<SysRoleDTO> getSysRoleByUserId(String userId) throws Exception{
        SysRoleDTO sysRoleDTO = new SysRoleDTO();
        sysRoleDTO.setTarget_id(Long.parseLong(userId));
        List<SysRoleDTO> sysRoleDTOList = sysRoleService.selectSysRoleByUserId(sysRoleDTO);
        return sysRoleDTOList;
    }

    /**
     * 根据用户id查询用户的按钮权限,用于按钮的显示和隐藏
     * @param userId
     * @return
     */
    private List<SysResourceDTO> getPermission(String userId) throws Exception {
        SysResourceDTO rolePermissionParam = new SysResourceDTO();
        rolePermissionParam.setUserId(Long.parseLong(userId));
        rolePermissionParam.setResourceType("button");
        List<SysResourceDTO> sysResourceDTOList = sysResourceService.selectShiroUrlPermissionByUserId(rolePermissionParam);
        return sysResourceDTOList;

    }

    /**
     * 根据userid查询菜单信息
     * @param userId
     * @return
     */
    private List<SysMenuDTO> getSysMenuByUserId(String userId) throws Exception {
        List<SysMenuDTO> sysMenuDTOList = sysMenuService.querySysMenuByUserId(userId);
        return sysMenuDTOList;
    }


    /**
     * 转换菜单为菜单树
     * @param sysMenuSourceList
     * @return
     */
    private List<SysMenuVo> convertSysMenuVo(List<SysMenuVo> sysMenuSourceList) throws Exception{
        //查询一级菜单
        List<SysMenuVo> roots = this.searchMenuByParentId(sysMenuSourceList,"0");

        this.convertSysMenuVo(roots,sysMenuSourceList);
        return roots;
    }

    private void convertSysMenuVo(List<SysMenuVo> roots, List<SysMenuVo> sysMenuSourceList) {
        //遍历一级菜单
        for(SysMenuVo root: roots) {
            String id = root.getId().toString();
            List<SysMenuVo> childs = this.searchMenuByParentId(sysMenuSourceList, id);
            root.setSysMenuVoChild(childs);
            convertSysMenuVo(childs,sysMenuSourceList);
        }
    }

    /**
     * 根据parentid查询子节点
     * @param sysMenuVoList
     * @param parentId
     * @return
     */
    private List<SysMenuVo> searchMenuByParentId(List<SysMenuVo> sysMenuVoList, String parentId) {
        List<SysMenuVo> childs = new ArrayList<>();
        for(SysMenuVo sysMenuVo: sysMenuVoList) {
            if(sysMenuVo.getParentId().equals(parentId)) {
                childs.add(sysMenuVo);
            }
        }
        return childs;

    }


    public Object searchSysUser(SysUserParam obj) throws Exception{
        Map<String,Object> params = new HashMap<>();
        params.put("dto",obj);
        List<SysUserDTO> result = sysUserDao.searchSysUser(params);
        return result;
    }

    public SysUserDTO queryLikeSysUser(SysUserDTO obj) throws Exception{
        Map<String,Object> params = new HashMap();
        params.put("dto",obj);
        return sysUserDao.queryLikeSysUser(params);
    }
}
